<%@ taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2022-04-22
  Time: 16:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"
isELIgnored="false" %>
<html>
<head>
    <title>注册账号</title>
    <script type="text/javascript">
        function submitForm() {
            let username = document.getElementById("username").value;
            let password = document.getElementById("password").value;
            let valid = document.getElementById("valid").value;
            if(username===null || username === "") {
                alert("用户名不能为空")
                return;
            }
            if(password===null || password === "") {
                alert("密码不能为空")
                return;
            }
            let targets = document.getElementsByName("role");
            for (let i = 0; i < targets.length; i++) {
                if(targets[i].checked) {
                    var target = targets[i].value;
                }
            }
            document.forms['register_form'].action = "${pageContext.request.contextPath}/" + target + "/register";
            document.forms['register_form'].submit();
        }
    </script>
    <style>
        .errorForm{
            color: #ff0000;
        }
    </style>
</head>
<body>
<mvc:form name="register_form" modelAttribute="user" action="register" method="post">
<table>
    <tr>
        <td colspan="2"><a href="${pageContext.request.contextPath}/user/hello">返回登录</a></td>
    </tr>
    <tr>
        <td><mvc:label path="username">Username</mvc:label></td>
        <td><mvc:input path="username" id="username"/></td>
        <td><span id="usernameError" class="errorForm">${param.usernameError}</span></td>
    </tr>
    <tr>
        <td><mvc:label path="password">Password</mvc:label></td>
        <td><mvc:password path="password" id="password"/></td>
        <td><span id="passwordError" class="errorForm">${param.passwordError}</span></td>
    </tr>
    <tr>
        <td><label for="valid">Valid</label></td>
        <td><input type="password" id="valid" name="valid"/></td>
    </tr>
    <tr>
        <td><label>用户
            <input type="radio" name="role" value="user" checked>
        </label></td>
        <td><label>作者
            <input type="radio" name="role" value="author">
        </label></td>
        <td><label>管理员
            <input type="radio" name="role" value="admin">
        </label></td>
    </tr>
    <tr>
        <td colspan="2"><input type="button" value="注册" onclick="submitForm()"></td>
    </tr>
</table>
</mvc:form>
</body>
</html>
